import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {


    static class TreeNode{
        public char ch;
        public TreeNode left ;
        public TreeNode right ;

        public TreeNode(char ch) {
            this.ch = ch;
        }
    }

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        F.right = H;
        return A;
    }

    //前序遍历：根->左->右
    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.ch+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历：左->根->右
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.ch+" ");
        inOrder(root.right);
    }

    //后续遍历：左->右->根
    public void posOrder(TreeNode root){
        if(root == null){
            return;
        }

        posOrder(root.left);
        posOrder(root.right);
        System.out.print(root.ch+" ");
    }

    // 获取树中节点的个数
    public int nodeSum(TreeNode root){
        if(root == null){
            return 0;
        }
        return nodeSum(root.left) + nodeSum(root.right) + 1;
    }

    // 获取叶子节点的个数
    public int leafNodeSum(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return leafNodeSum(root.left) + leafNodeSum(root.right);
    }

    // 获取第K层节点的个数
    public int kLevelNode(TreeNode root , int k){
        if(root == null){
            return 0;
        }

        if( k == 1){
            return 1;
        }

        int left = kLevelNode(root.left,k-1);
        int right = kLevelNode(root.right,k-1);

        return left + right;
    }

    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }

        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return (leftHeight > rightHeight ? leftHeight : rightHeight ) + 1;
    }

    // 检测值为value的元素是否存在
    public boolean findValue(TreeNode root , char value){
        if(root == null){
            return false;
        }
        if(root.ch == value){
            return true;
        }
        boolean findLeft = findValue(root.left,value);
        if(findLeft){
            return findLeft;
        }
        boolean findRight = findValue(root.right,value);
        if(findRight){
            return findRight;
        }
        return false;
    }

    //层序遍历
    public void levelOrder1(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.ch + " ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }
    }

    public boolean isSameTree(TreeNode root1, TreeNode root2){
        if(root1 == null && root2 == null){
            return true;
        }
        if(root1 == null && root2 != null || root1 != null && root2 == null){
            return false;
        }
        if(root1.ch != root2.ch){
            return false;
        }
        boolean left = isSameTree(root1.left,root2.left);
        boolean right = isSameTree(root1.right,root2.right);

        if(left && right){
            return true;
        }
        return false;
    }

    //修改：
    public boolean isSameTree1(TreeNode p , TreeNode q){
        if(p == null && q != null || p != null && q == null){
            return false;
        }
        //之后比较的两颗二叉树的节点，要么空，要么都不空
        if(p == null && q == null){
            return true;
        }

        if(p.ch != q.ch){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right); //这里利用了 && 短路的技巧，即第一个为假，则后面的不再运行
    }

    public boolean isSubTree(TreeNode root,TreeNode sub){
        if(isSameTree1(root,sub)){
            return true;
        }
        if(root == null && sub != null){
            return false;
        }
        return isSubTree(root.left,sub) || isSubTree(root.right,sub);
    }

    //翻转一颗二叉树
    //思路：每个节点的左子树右子树交换
    public TreeNode invertTree(TreeNode root){
        if(root == null){
            return null;
        }
        //如果不加下面的判断条件的话，那么叶节点的左右节点还会交换。
        if(root.left == null && root.right == null){
            return root;
        }

        TreeNode cur = null;
        cur = root.left;
        root.left = root.right;
        root.right = cur;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //一颗高度平衡的二叉树：一个二叉树每个节点的左右两个子树的高度差的绝对值不超过 1 。
    //看答案写出来的：
    //⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
    public boolean isBalanced(TreeNode root){
        return getHeight1(root) >= 0 ;
    }
    public int getHeight1(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = getHeight1(root.left);
        if(leftHeight < 0){
            return -1;
        }
        int rightHeight = getHeight1(root.right);
        if(rightHeight < 0 ){
            return -1;
        }
        if(Math.abs(leftHeight - rightHeight) < 2 ) {
            return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
        }else{
            return -1;
        }
    }

    //判断二叉树是否对称
    //根节点的左子树和右子树进行比较
    public boolean isSymmetric(TreeNode root){
        if(root == null){
            return true;
        }

        return isChildSymmetric(root.left, root.right);
    }

    public boolean isChildSymmetric(TreeNode leftRoot,TreeNode rightRoot){
        if(leftRoot != null && rightRoot == null || leftRoot == null && rightRoot != null){
            return false;
        }
        if(rightRoot == null && leftRoot == null){
            return true;
        }
        if(leftRoot.ch != rightRoot.ch){
            return false;
        }

        return isChildSymmetric(leftRoot.left,rightRoot.right) && isChildSymmetric(leftRoot.right,rightRoot.left);
    }

    //二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
        if(root == null){
            return null;
        }

        if(root == q || root == p){
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);

        if(left != null && right != null){
            return root;
        }else if(left != null){
            return left;
        }else if(right != null){
            return right;
        }else{
            return null;
        }
    }


}
